import QQMapWX from '@/utils/qqmap-wx-jssdk.min.js'
let qqmapsdk = new QQMapWX({
	key: '7J2BZ-W5PR3-EDH3E-YC2LT-OFPG3-O2FJY'
})
let fun = ''
let getLocation = function(state = '') {
	fun = state
	return new Promise((reslove) => { //获取设置
		uni.getSetting({
			success(res) {
				if (res.authSetting["scope.userLocation"] || res.authSetting[
						"scope.userLocation"] === undefined) { //获取定位
					uni.getLocation({
						type: 'gcj02',
						success(res) {
							reslove(codeAroundCity(res.latitude, res.longitude))
						},
						fail(err) {
							reslove(codeAroundCity())
						}
					})
				} else {
					uni.showModal({
						title: '提示',
						content: '为方便更好的服务您，需要获取您的位置信息！',
						success(res) {
							if (res.confirm) { //打开设置
								uni.openSetting({
									success(dataAu) {
										if (dataAu.authSetting[
												'scope.userLocation'] === true) {
											uni.getLocation({
												type: 'gcj02',
												success(res) {
													reslove(codeAroundCity(
														res
														.latitude,
														res
														.longitude))
												},
												fail(err) {
													console.log(err);
												}
											})
										}

									}
								})
							} else {
								reslove(codeAroundCity())
							}
						}
					})
				}
			}
		})
	})

}

let codeAroundCity = function(latitude = 37.870472, longitude = 112.548877) {
	return new Promise((reslove, reject) => {
		uni.chooseLocation({
			latitude,
			longitude,
			success: function(res) {
				uni.hideLoading()
				if(fun == 'qq') {
					qqmapsdk.reverseGeocoder({
						location: {
							latitude: res.latitude,
							longitude: res.longitude
						},
						success: function(res2) {
							reslove({
								info: res2.result.ad_info,
								address: res
							})
						},
					});
				}else {
					reslove(res)
				}
				
			},
			fail: err => {
				uni.hideLoading()
			}
		});

	})

}
export default getLocation